/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.log.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.log.entity.IntegralYearLogEntity;
import org.springblade.modules.log.vo.IntegralYearLogVO;
import org.springblade.modules.log.excel.IntegralYearLogExcel;
import org.springblade.modules.log.mapper.IntegralYearLogMapper;
import org.springblade.modules.log.service.IIntegralYearLogService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import java.util.List;

/**
 * 年度积分/学时获取记录 服务实现类
 *
 * @author junxin
 * @since 2023-06-13
 */
@Service
public class IntegralYearLogServiceImpl extends BaseServiceImpl<IntegralYearLogMapper, IntegralYearLogEntity> implements IIntegralYearLogService {

	@Override
	public IPage<IntegralYearLogVO> selectIntegralYearLogPage(IPage<IntegralYearLogVO> page, IntegralYearLogVO integralYearLog) {
		return page.setRecords(baseMapper.selectIntegralYearLogPage(page, integralYearLog));
	}


	@Override
	public List<IntegralYearLogExcel> exportIntegralYearLog(Wrapper<IntegralYearLogEntity> queryWrapper) {
		List<IntegralYearLogExcel> integralYearLogList = baseMapper.exportIntegralYearLog(queryWrapper);
		//integralYearLogList.forEach(integralYearLog -> {
		//	integralYearLog.setTypeName(DictCache.getValue(DictEnum.YES_NO, IntegralYearLog.getType()));
		//});
		return integralYearLogList;
	}

	@Override
	public boolean addYearIntegral(String userNumber,String userName, String year, float integral, float period) {
		IntegralYearLogEntity integralYearLogEntity = this.getOne(Wrappers.<IntegralYearLogEntity>query().lambda()
			.eq(IntegralYearLogEntity::getUserNumber, userNumber)
			.eq(IntegralYearLogEntity::getYear, year));
		if (null==integralYearLogEntity){
			integralYearLogEntity=new IntegralYearLogEntity();
			integralYearLogEntity.setUserNumber(userNumber);
			integralYearLogEntity.setUserName(userName);
			integralYearLogEntity.setYear(year);
			integralYearLogEntity.setPeriod(period);
			integralYearLogEntity.setIntegral(integral);
			integralYearLogEntity.setIntegralTarget((float) 0);
			return this.save(integralYearLogEntity);
		}else {
			Float oldPeriod = integralYearLogEntity.getPeriod();
			Float oldIntegral = integralYearLogEntity.getIntegral();
			Float oldIntegralTarget = integralYearLogEntity.getIntegralTarget();
			if (null==oldPeriod || oldPeriod<0) oldPeriod=(float)0;
			if (null==oldIntegral || oldIntegral<0) oldIntegral=(float)0;
			if (null==oldIntegralTarget || oldIntegralTarget<0) oldIntegralTarget=(float)0;
			integralYearLogEntity.setPeriod(oldPeriod+period);
			integralYearLogEntity.setIntegral(oldIntegral+integral);
			integralYearLogEntity.setIntegralTarget(oldIntegralTarget);
			return this.updateById(integralYearLogEntity);
		}
	}

}
